﻿using System.Reflection;
using System.Runtime.Loader;

namespace EasyOAuth
{
    /// <summary>
    /// 批量注入服务
    /// </summary>
    public static class RegisterAssemblyExtensions
    {
        /// <summary>
        /// 注入dll文件 需要接口模式
        /// </summary>
        /// <param name="service"></param>
        /// <param name="assemblyName"></param>
        /// <param name="suffixName">注入的服务类后辍名 默认 Service 比如 IDLLService</param>
        /// <param name="serviceLifetime"></param>
        public static void RegisterAssemblyServiceAsInterface(this IServiceCollection service,
            string assemblyName,
            string suffixName = "Service",
            ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));

            var types = assembly.GetTypes();
            var list = types.Where(u => u.IsClass && !u.IsAbstract && !u.IsGenericType && !u.IsInterface)
                .Where(t => t.Name.EndsWith(suffixName)).ToList();

            foreach (var type in list)
            {
                var interfaceList = type.GetInterfaces();
                if (interfaceList.Any())
                {
                    var inter = interfaceList.First();

                    switch (serviceLifetime)
                    {
                        case ServiceLifetime.Transient:
                            service.AddTransient(inter, type);
                            break;
                        case ServiceLifetime.Scoped:
                            service.AddScoped(inter, type);
                            break;
                        case ServiceLifetime.Singleton:
                            service.AddSingleton(inter, type);
                            break;

                    }
                    service.AddScoped(inter, type);
                }
            }
        }


        /// <summary>
        /// 注入DLL文件 服务模式 只需要定义服务类即可
        /// </summary>
        /// <param name="service"></param>
        /// <param name="assemblyName">注入的dll名称</param>
        /// <param name="suffixName">注入的服务类后辍名 默认 Service 比如 UserService</param>
        /// <param name="serviceLifetime">注入周期</param>
        public static void RegisterAssemblyService(this IServiceCollection service,
            string assemblyName,
            string suffixName = "Service",
            ServiceLifetime serviceLifetime = ServiceLifetime.Scoped)
        {
            var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(assemblyName));

            var types = assembly.GetTypes();
            var list = types.Where(u => u.IsClass && !u.IsAbstract && !u.IsGenericType && !u.IsInterface)
                .Where(t => t.Name.EndsWith(suffixName)).ToList();

            foreach (var type in list)
            {
                switch (serviceLifetime)
                {
                    case ServiceLifetime.Transient:
                        service.AddTransient(type);
                        break;
                    case ServiceLifetime.Scoped:
                        service.AddScoped(type);
                        break;
                    case ServiceLifetime.Singleton:
                        service.AddSingleton(type);
                        break;
                }
            }
        }
    }
}
